package com.lepu.app.fun.my;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.app.utils.LoginUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.widget.MyDialog;
import com.lepu.app.usercenter.RegisterByQuestionActivity;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.app.usercenter.util.UserCenterUtil;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;
import com.lepu.pasm.R;

/**
 * 修改手机号
 * @author wxd
 *
 */
public class MyMobileActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener,OnTopbarNewRightButtonListener,AsyncRequest {

	private static final String REQUEST_SUBMIT = "request_submit";
	private static final String REQUEST_GET_CODE = "request_get_code";
	
	private static final int MSG_GET_CODE_SUCCESS = 20;
	private static final int MSG_GET_CODE_FAIL = 21;
	private static final int MSG_GET_CODE_UPDATE = 22;

	private static final int MSG_SUBMIT_SUCCESS = 30;
	private static final int MSG_SUBMIT_FAIL = 31;
	
	private Button mGetCodeButton;
	String mNewUserContent = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_mobile_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}
	
	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void onTopbarRightButtonSelected() {
		requestOk();
	}
	

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("验证手机号");
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setRightText("完成");
		topbar.setOnTopbarNewRightButtonListener(this);
		
		mGetCodeButton = (Button) findViewById(R.id.getCodeButton);
		TextView noCodeTextView = (TextView) findViewById(R.id.noCodeTextView);

		mGetCodeButton.setTag(0);
		mGetCodeButton.setOnClickListener(mOnClickListener);
		noCodeTextView.setOnClickListener(mOnClickListener);

		TextView currentPhoneTextView = (TextView) findViewById(R.id.currentPhoneTextView);
		currentPhoneTextView.setText(MyApplication.getInstance().getLoginInfo().MobilePhone);
	}

	private void requestOk()
	{
		String oldUserContent = MyApplication.getInstance().getLoginInfo().MobilePhone;

		EditText mobileEditText = (EditText) findViewById(R.id.mobileEditText);
		mNewUserContent = mobileEditText.getText().toString().trim();

		EditText codeEditText = (EditText) findViewById(R.id.codeEditText);
		String codeContent = codeEditText.getText().toString().trim();

		boolean flag = UserCenterUtil.getUpdateLoginID(this, this,
				REQUEST_SUBMIT, oldUserContent,mNewUserContent,codeContent);
		if (flag) {
			showProgressDialog();
		}
	}
	
	private void requestGetCode() {
		EditText userEditText = (EditText) findViewById(R.id.mobileEditText);
		String userContent = userEditText.getText().toString().trim();

		boolean flag = UserCenterUtil.getCommonCode(this, this,
				REQUEST_GET_CODE, userContent);
		if (flag) {
			showProgressDialog();
		}
	}
	
	private void checkCode() {

		EditText userEditText = (EditText) findViewById(R.id.mobileEditText);

		final String userContent = userEditText.getText().toString().trim();

		if (TextUtils.isEmpty(userContent) || userContent.length() != 11) {
			UIHelper.showToast(MyMobileActivity.this,
					R.string.usercenter_user_msg);
			return;
		}

		new MyDialog(MyMobileActivity.this)
				.setMessage(R.string.usercenter_code_message)
				.setNegativeButton(R.string.usercenter_code_go,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								requestGetCode();
							}
						})
				.setPositiveButton(R.string.usercenter_code_jump,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {

								Intent intent = new Intent(
										MyMobileActivity.this,
										RegisterByQuestionActivity.class);
								intent.putExtra("phone", userContent);
								startActivity(intent, true);
							}
						}).create(null).show();
	}
	
	View.OnClickListener mOnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.getCodeButton:

//				int tag = (Integer) v.getTag();
//				if (tag == 0) {
//					requestGetCode();
//				} else {
//					checkCode();
//				}
				// 不需要跳过验证
				 requestGetCode();
				break;
			case R.id.noCodeTextView:

				checkCode();
				break;
			
			default:
				break;
			}
		}
	};
	
	@SuppressLint("HandlerLeak") 
	Handler mHandler = new Handler(){
		
		public void handleMessage(Message msg) {
			
			switch (msg.what) {
			case MSG_GET_CODE_UPDATE:

				int codeText = Integer.parseInt(mGetCodeButton.getText()
						.toString());
				codeText--;

				mGetCodeButton.setText(codeText + "");
				if (codeText <= 0) {
					mHandler.removeMessages(MSG_GET_CODE_UPDATE);
					mGetCodeButton.setTag(1);
					mGetCodeButton.setText("重新获取验证码");
					mGetCodeButton.setEnabled(true);
				} else {
					mHandler.sendEmptyMessageDelayed(MSG_GET_CODE_UPDATE, 1000);
				}

				break;
			case MSG_GET_CODE_SUCCESS:
				hideProgressDialog();

				mHandler.removeMessages(MSG_GET_CODE_UPDATE);
				mGetCodeButton.setTag(0);
				mGetCodeButton.setEnabled(false);
				mGetCodeButton.setText("60");
				mHandler.sendEmptyMessageDelayed(MSG_GET_CODE_UPDATE, 1000);
				break;
			case MSG_GET_CODE_FAIL:
				hideProgressDialog();
				break;
			case MSG_SUBMIT_SUCCESS:

				hideProgressDialog();
				
				LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
				loginInfo.MobilePhone = mNewUserContent;
				LoginInfo.saveLoginInfo(MyMobileActivity.this, loginInfo);
				
				finish(true);
				break;
			case MSG_SUBMIT_FAIL:

				hideProgressDialog();
				break;

			default:
				break;
			}
		};
	};

	@Override
	public void RequestComplete(Object object, Object data){
		if(object.equals(REQUEST_SUBMIT))
		{
			mHandler.sendEmptyMessage(MSG_SUBMIT_SUCCESS);
		}else if(object.equals(REQUEST_GET_CODE))
		{
			mHandler.sendEmptyMessage(MSG_GET_CODE_SUCCESS);
		}
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
		
		if(object.equals(REQUEST_SUBMIT))
		{
			mHandler.sendEmptyMessage(MSG_SUBMIT_FAIL);
		}else if(object.equals(REQUEST_GET_CODE))
		{
			mHandler.sendEmptyMessage(MSG_GET_CODE_FAIL);
		}
		
	}

}
